var GameID=null;
var mainStatsObj=null;
jQuery(document).ready(function($)
{
$(".js-hidden").hide();
    mainStatsObj = new MainStats();
    GameID = $('input[name="gameID"]').val();
    $('#ticker').marquee('pointer').mouseover(function () {

        $(this).trigger('pause');
    }).mouseout(function () {
        $(this).trigger('start');
    });
    // previousHash = ( previousHash.replace( /^#/, '' ) || 'blank' );

    $(window).hashchange( function(){
        var hash = location.hash;

        // Set the page title based on the hash.
        var currentHash = ( hash.replace( /^#/, '' ) || 'blank' );
        if (currentHash == 'blank')
        {
            xhrMainStats.abort();
            xhrMatchStats.abort();
            xhrScoreCard.abort();
        }

    });
    //

    //
    waitForMainStats();
    getMatchStats();
  //  waitForLiveView();
waitForScoreCardStats();
//waitForMatchStats();
prepareCanvas();
setUpDeliveryInfoTicker();

});




function setUpWorm()
{
    var chart;

    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'wormContainer',
            // defaultSeriesType: 'line',
            marginRight: 130,
            marginBottom: 25
        },
        title: {
            text: 'Worm (Fall of Wickets)',
            x: -20 //center
        },
        subtitle: {
            text: mainStatsObj.getFirstInningsBatTeam()+" Vs. "+mainStatsObj.getSecondInningsBatTeam(),
            x: -20
        },
        xAxis: {
            title: {
                text: 'Over'
            },
            categories: fallOfWicketsFirstInningsOvers
        },
        yAxis: {
            title: {
                text: 'Runs'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function() {
                if (this.series.name == mainStatsObj.getFirstInningsBatTeam())
                {
                    var index = fallOfWicketsFirstInningsRuns.indexOf(this.y);
                    return '<b>'+ this.series.name +'</b><br/>'+
                    fallOfWicketsFirstInningsBatsman[index]+ " was dismissed at " +this.x +' over <br> when '+this.series.name+" had made "+ this.y +' runs';
                }
                else if (this.series.name == mainStatsObj.getSecondInningsBatTeam())
                {
                    var index = fallOfWicketsSecondInningsRuns.indexOf(this.y);
                    return '<b>'+ this.series.name +'</b><br/>'+
                    fallOfWicketsSecondInningsBatsman[index]+ " was dismissed at " +this.x +' over <br> when '+this.series.name+" had made "+ this.y +' runs';
                }


            } ,

            crosshairs: true
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        credits: {
            enabled: false
        },

        series: [{
            name: mainStatsObj.getFirstInningsBatTeam(),
            data: fallOfWicketsFirstInningsRuns
        }, {
            name: mainStatsObj.getSecondInningsBatTeam(),
            data: fallOfWicketsSecondInningsRuns
        }]
    });






}
function setUpRunTypeChart()
{
    var runsTypeChartInningsOne;

    runsTypeChartInningsOne = new Highcharts.Chart({
        chart: {
            renderTo: 'runsTypeContainerFirstInnings',
            defaultSeriesType: 'bar'
        },
        title: {
            text: 'Run Type Distribution - First Innings'
        },
        subtitle: {
            text: mainStatsObj.getFirstInningsBatTeam()
        },
        xAxis: {
            categories: ['Ones', 'Twos', 'Threes', 'Fours', 'Fives','Sixes'],
            title: {
                text: "Type of Run"
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Number of Runs',
                align: 'high'
            }
        },
        tooltip: {
            formatter: function() {
                return ''+
                this.series.name +': '+ this.y;
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -100,
            y: 100,
            floating: true,
            borderWidth: 1,
            backgroundColor:  '#FFFFFF',
            shadow: true
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'Shots Played',
            data: runsTypeFirstInnArray
        }]
    });
    if (mainStatsObj.Innings == "2")
    {
        var runsTypeChartInningsTwo;

        runsTypeChartInningsTwo = new Highcharts.Chart({
            chart: {
                renderTo: 'runsTypeContainerSecondInnings',
                defaultSeriesType: 'bar'
            },
            title: {
                text: 'Run Type Distribution - Second Innings'
            },
            subtitle: {
                text: mainStatsObj.getSecondInningsBatTeam()
            },
            xAxis: {
                categories: ['Ones', 'Twos', 'Threes', 'Fours', 'Fives','Sixes'],
                title: {
                    text: "Type of Run"
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Number of Runs',
                    align: 'high'
                }
            },
            tooltip: {
                formatter: function() {
                    return ''+
                    this.series.name +': '+ this.y;
                }
            },
            plotOptions: {
                bar: {
                    dataLabels: {
                        enabled: true
                    }
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -100,
                y: 100,
                floating: true,
                borderWidth: 1,
                backgroundColor:  '#FFFFFF',
                shadow: true
            },
            credits: {
                enabled: false
            },
            series: [{
                name: 'Shots Played',
                data: runsTypeSecondInnArray
            }]
        });
    }


}


function setUpMahattan()
{

    var manhattanChartInningsOne;

    manhattanChartInningsOne = new Highcharts.Chart({
        chart: {
            renderTo: 'manhattanContainerFirstInnings',
            defaultSeriesType: 'column'
        },
        title: {
            text: 'Manhattan - First Innings'
        },
        subtitle: {
            text: mainStatsObj.getFirstInningsBatTeam()
        },
        xAxis: {
            categories: manhattanFirstInningOvers,
            title: {
                text: 'Over'
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Runs/Wickets'
            },
            stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }
        },
        legend: {
            align: 'right',
            x: -100,
            verticalAlign: 'top',
            y: 20,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
            borderColor: '#CCC',
            borderWidth: 1,
            shadow: false
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                this.series.name +': '+ this.y +'<br/>'+
                'Total: '+ this.point.stackTotal;
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: true,
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                }
            }
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'Runs',
            data: manhattanFirstInningRuns
        }, {
            name: 'Wickets',
            data: manhattanFirstInningWickets
        }]
    });
    if (mainStatsObj.Innings == "2")
    {
        var manhattanChartInningsTwo;
        manhattanChartInningsOne = new Highcharts.Chart({
            chart: {
                renderTo: 'manhattanContainerSecondInnings',
                defaultSeriesType: 'column'
            },
            title: {
                text: 'Manhattan - Second Innings '
            },
            subtitle: {
                text: mainStatsObj.getSecondInningsBatTeam()
            },
            xAxis: {
                categories: manhattanSecondInningOvers,
                title: {
                    text: 'Over'
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Runs/Wickets'
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            legend: {
                align: 'right',
                x: -100,
                verticalAlign: 'top',
                y: 20,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                borderColor: '#CCC',
                borderWidth: 1,
                shadow: false
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                    }
                }
            },
            credits: {
                enabled: false
            },
            series: [{
                name: 'Runs',
                data: manhattanSecondInningRuns
            }, {
                name: 'Wickets',
                data: manhattanSecondInningWickets
            }]
        });
    }



}